<?php

include 'AppConfig.php';

class DbConnector
{
    public static function Connect()
    {
        mysql_connect(AppConfig::$dbhost, AppConfig::$dbusername);
        mysql_select_db(AppConfig::$dbname) or die('Cannot select database'); 
    }
    
    public static function Disconnect()
    {
        mysql_close();
    }
};

class WebHelper
{
    public static function GetCleanString($text)
    {
        // TODO: Check for any ambigious characters
        return $text;
    }
    
    public static function GetString($key)
    {
        // TODO: Check for any ambigious characters
         if($_GET[$key] == NULL)
            throw new Exception ('Error in the passed paramaters.');
        
        return $_GET[$key];
    }
    
    public static function GetInt($key)
    {
        // TODO: Check for any ambigious characters
        if($_GET[$key] == NULL)
            throw new Exception ('Error in the passed paramaters.');
        
        return $_GET[$key];
    }
    
    public static function GetFload($key)
    {
        if($_GET[$key] == NULL)
            throw new Exception ('Error in the passed paramaters.');
        
        return $_GET[$key];
    }
    
    public static function PostString($key)
    {
        if($_POST[$key] == NULL)
            throw new Exception ('Error in the passed paramaters.');
        
        return $_POST[$key];
    }

    public static function PostInt($key)
    {
        if($_POST[$key] == NULL)
            throw new Exception ('Error in the passed paramaters.');
        
        return $_POST[$key];
    }
   
    public static function PostFloat($key)
    {
        if($_POST[$key] == NULL)
            throw new Exception ('Error in the passed paramaters.');
        
        return $_POST[$key];
    }
     
    public static function IsPostKeyNotNull($key)
    {
        if($_POST[$key] == NULL)
            return false;
        
        return true;
    }

    public static function IsGetKeyNotNull($key)
    {
        if($_GET[$key] == NULL)
        {
            return false;
        }        
            
        return true;
    }
    
    public static function Redirect($virtualUrl)
    {
        $rootUrl = AppConfig::$root;        
        $redirectUrl = "Location:$rootUrl$virtualUrl";
        //echo($redirectUrl);
        header("$redirectUrl");
    }
    
    public static function ResolveUrl($virtualUrl)
    {
        return AppConfig::$root . $virtualUrl;
    }
    
    public static function IsPostBack()
    {
        if($_POST["_ispostback"] == NULL)
        {
            return false;
        }
        
        return true;
    }
    
    public static function UploadFile($uploadInputName)
    {
        $fileNameWithExt = basename( $_FILES[$uploadInputName]['name']);       
        $ext = substr($fileNameWithExt, strrpos($fileNameWithExt, '.') + 1);        
        $fileName = md5(date(DATE_RFC822) . session_id()) . "." . $ext;        
        $target_path = AppConfig::$uploadPath;        
        $target_path = $target_path . $fileName; 
        
        //echo($target_path);
        
        
        if( move_uploaded_file($_FILES[$uploadInputName]['tmp_name'], $target_path) )
        {
            //echo "The file ".  basename( $_FILES[$uploadInputName]['name']). " has been uploaded.";
            return $fileName;
        }
        else
        {
            //echo "There was an error uploading the file, please try again!";
        }
        
        return "";
    }
    
    public static function ResizeImage()
    {
        $full_url = "../posterFull/".$id.".jpg";

        $imageInfo = getimagesize($imgfile);
        $src_width = $imageInfo[0];
        $src_height = $imageInfo[1];
                
        imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height);
        
        imagejpeg($dst_img,$full_url);
        
        imagedestroy($src_img);
    }
};

class HtmlHelper
{
    public static function Display($bool)
    {
        if($bool)
            return "inline";
        
        return "none";
    }
}

class Converter
{
    public static function ToBooleanString($bool)
    {
        if($bool)
            return "true";
        
        return "false";
    }
}

?>